package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统设置之角色管理控制层
 * @Author CHENG KAI
 * @Date 2020/4/16 14:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 查询所有角色信息
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 分页查询所有角色信息
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return roleService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 新增角色
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }
    /**
     * 根据角色id查询角色信息
     */
    @RequestMapping("/queryByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result queryByRoleId(Integer roleId){
        try {
            Role role = roleService.queryByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /**
     * 根据角色id查询关联的权限id数组
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findPermissionIdsByRoleId(Integer roleId){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
    /**
     * 根据角色id查询关联的菜单id数组
     */
    @RequestMapping("/findMenuIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result findMenuIdsByRoleId(Integer roleId){
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    /**
     * 编辑角色
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.edit(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
    /**
     * 根据id删除角色数据
     */
    @RequestMapping("/deleteByRoleId")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteByRoleId(Integer roleId){
        try {
            roleService.deleteByRoleId(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
